﻿Imports System.IO
Imports System.Web.Mvc
Imports ADODBU
Public Class ProcessHelper
    Public Interface iProcessGeneric
        Function CreateProcess(ByVal User As String, ByVal Target As String, ByVal param As String) As JsonResult
        Function IsRunning() As Boolean
        Function GetStatus() As String
    End Interface

    Public MustInherit Class AbsProcess
        Implements iProcessGeneric

        Protected mIsRunning As Boolean = False


        Public MustOverride Function CreateProcess(ByVal User As String, ByVal Target As String, ByVal param As String) As System.Web.Mvc.JsonResult Implements iProcessGeneric.CreateProcess


        Public MustOverride Function GetStatus() As String Implements iProcessGeneric.GetStatus



        Public Function IsRunning() As Boolean Implements iProcessGeneric.IsRunning
            Return mIsRunning
        End Function

    End Class

    Public Class ProcessHelper
        Shared mListReport As ListReport
        Shared Sub InitThis()
            Try
                If Dir(AppDomain.CurrentDomain.BaseDirectory & "Models\ProcessList.txt") <> "" Then
                    Dim sr As StreamReader = New StreamReader(AppDomain.CurrentDomain.BaseDirectory & "Models\ProcessList.txt")
                    Dim cdata As String
                    Dim arr() As String
                    mListReport = New ListReport
                    Do While sr.Peek() >= 0
                        cdata = sr.ReadLine()
                        arr = cdata.Split(";")
                        mListReport.AddItem(arr(0), New DataObj With {.FileName = arr(1), .ClassName = arr(2)})
                    Loop
                    sr.Close()
                End If
            Catch
            End Try

        End Sub

        Shared Function CreateProcess(ByVal param As String) As JsonResult
            Dim x As DataObj
            If mListReport Is Nothing Then
                InitThis()
            End If

            Dim vd As New ProcessParam
            RowClassUtil.RowClassFill(vd, param)

            x = mListReport.Item(vd.ModuleID)
            If Not x Is Nothing Then
                Dim xr As iProcessGeneric
                xr = GetProcess(x)
                If xr Is Nothing Then
                    Return Nothing
                Else
                    Return xr.CreateProcess(vd.UserID, vd.ModuleID, vd.Params)
                End If
            Else
                Return Nothing
            End If
        End Function

        Shared Function GetProcess(ByVal dta As DataObj) As iProcessGeneric
            Dim currDomain As AppDomain
            Dim TblData As iProcessGeneric
            Dim objHandle As System.Runtime.Remoting.ObjectHandle
            Try

                currDomain = AppDomain.CurrentDomain
                objHandle = currDomain.CreateInstanceFrom(AppDomain.CurrentDomain.BaseDirectory & "bin\" & dta.FileName, dta.ClassName)
                TblData = objHandle.Unwrap()
                Return TblData

            Catch e As Exception
                Return Nothing
            End Try
        End Function
    End Class

    Public Class ProcessParam
        Inherits ADODBU.RowClass
        Dim mtbl As String
        Dim mSource As String
        Dim mFile As String
        Dim _TableName As String
        Public Property UserID() As String
            Get
                Return _TableName
            End Get
            Set(ByVal value As String)
                _TableName = value
            End Set
        End Property
        Dim _FileName As String
        Public Property ModuleID() As String
            Get
                Return _FileName
            End Get
            Set(ByVal value As String)
                _FileName = value
            End Set
        End Property

        Dim _ObjName As String
        Public Property Params() As String
            Get
                Return _ObjName
            End Get
            Set(ByVal value As String)
                _ObjName = value
            End Set
        End Property
    End Class

End Class
